<?php

namespace app\company\service;

use app\company\model\CompanyCustomerModel;
use PhpOffice\PhpSpreadsheet\Writer\Exception;
use think\db\exception\DbException;
use think\Paginator;

class CustomerService extends Base
{

    /**
     * 获取表格数据
     * @param array $params
     * @return Paginator
     * @throws DbException
     */
    public function getList(array $params): Paginator
    {
        $model = new CompanyCustomerModel();
        $where = [];

        if(!empty($params['cid'])){
            $where['cid'] = $params['cid'];
        }
        if(!empty($params['name'])){
            $where['name'] = ['like', "%{$params['name']}%"];
        }

        //回收站
        if(!empty($params['is_recycle'])){
            $model = $model->onlyTrashed();
        }

        return $model->where($where)
            ->order('id', 'desc')
            ->paginate($params['pageSize']?? 10);
    }

    /**
     * 导出表格
     * @param array $params
     * @return array
     * @throws Exception|DbException
     */
    public function exportExcel(array $params): array
    {
        $model = new CompanyCustomerModel();
        $where = [];

        if(!empty($params['cid'])){
            $where['cid'] = $params['cid'];
        }
        if(!empty($params['name'])){
            $where['name'] = ['like', "%{$params['name']}%"];
        }

        //回收站
        if(!empty($params['is_recycle'])){
            $model = $model->onlyTrashed();
        }

        $list = $model->where($where)
            ->order('id', 'desc')
            ->select();

        $es = new \sword\service\ExcelService();
        $es->setTitle('客户清单')
            ->setCols([
                '客户名称' => 15,
                '客户简称' => 10,
                '联系人' => 10,
                '职位名称' => 15,
                '联系电话' => 14,
                '地址' => 15,
                '分级' => 10,
                '类别' => 10,
                '状态' => 10,
            ]);

        foreach ($list as $item){
            $es->writeLine([
                $item['name'],
                $item['simple_name'],
                $item['tel_name'],
                $item['role_name'],
                $item['tel_phone'],
                $item['address'],
                $item['class_name'],
                $item['type'],
                $item['status']? '启用': '禁用'
            ]);
        }

        return $es->saveToFile('导出客户清单');
    }

    /**
     * 新增
     * @param array $data
     * @return CompanyCustomerModel
     */
    public function add(array $data): CompanyCustomerModel
    {
        $model = new CompanyCustomerModel();
        //新增
        $model->save($data);
        return $model;
    }

    /**
     * 编辑
     * @param array $data
     * @return CompanyCustomerModel
     * @throws DbException
     */
    public function edit(array $data): CompanyCustomerModel
    {
        $model = CompanyCustomerModel::where('id', $data['id'])->find();
        $model->save($data);
        return $model;
    }

    /**
     * 批量删除
     * @param $ids
     * @param $isRecycle bool 是否强制删除
     * @return bool
     */
    public function delete($ids, bool $isRecycle = false): bool
    {
        if(!is_array($ids)) $ids = [$ids];
        foreach ($ids as $id) {
            CompanyCustomerModel::destroy($id, $isRecycle);
        }
        return true;
    }

    /**
     * 还原回收站
     * @param $id
     * @return bool
     * @throws DbException
     */
    public function reduction($id): bool
    {
        return CompanyCustomerModel::onlyTrashed()
            ->find($id)
            ->restore();
    }
}